import {Model} from './model.js';
var model =new Model();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bank: {
      'bank': '',
      'card_number': '',
      'name': ''
    },
    submit_flag:false
  },
  onLoad:function(options){
    model.getUserBank((res)=>{
      if(res.data.bank==null){
        res.data.bank=this.data.bank;
      }
      this.setData(res.data)
    })
  },
  // 回首页
  clickHome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 提交订单
  formSubmit: function (e) {
    if (this.data.submit_flag){   //防止重复提交
      return ;
    }
    var earnings = parseInt(this.data.earnings) ;  //收入余额
    var value = e.detail.value;
    var bank = value["bank"];
    var card_number = value["card_number"];
    var name = value["name"];
    var money = value["money"];
    var mobile = value["mobile"];
    if (bank.length<=0){
      wx.showToast({
        title: '开户银行必填', icon: 'none'
      })
      return;
    } 
    if (card_number.length <= 0) {
      wx.showToast({
        title: '银行卡号必填', icon: 'none'
      })
      return;
    } 
    if (name.length <= 0) {
      wx.showToast({
        title: '持卡人必填', icon: 'none'
      })
      return;
    } 
    if (money.length <= 0) {
      wx.showToast({
        title: '提现金额必填', icon: 'none'
      })
      return;
    } 

    if (earnings < money){
      wx.showToast({
        title: '收入余额不足', icon: 'none'
      })
      return;
    }

    if (mobile.length <= 0) {
      wx.showToast({
        title: '手机号必填', icon: 'none'
      })
      return;
    } 
    this.data.submit_flag=true;
    // 申请提现
    model.setEarningsDrawing(value,(res)=>{
      var statusCode = res.statusCode;
      if (statusCode == 200) {
        wx.showToast({
          title: res.data.msg, icon: 'none'
        });
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/earnings/index',
          });
        }, 2000);
      } else {
        wx.showToast({
          title: res.data.msg, icon: 'none'
        });
      }
    })

  }

})